// NotificationsService.cs
// Facebook/Framework/Service
// Copyright (c) 2007, Nikhil Kothari. All Rights Reserved.
//

using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.Text;
using Facebook.Service.Core;

namespace Facebook.Service {

    /// <summary>
    /// Represents the service APIs related to the user's notifications on Facebook.
    /// </summary>
    public sealed class NotificationsService {

        private FacebookSession _session;

        internal NotificationsService(FacebookSession session) {
            Debug.Assert(session != null);
            _session = session;
        }

        /// <summary>
        /// Sends a notification to the specified user, and optionally sends email.
        /// </summary>
        /// <param name="userID">The ID of the user to notify.</param>
        /// <param name="notification">The notification to send.</param>
        /// <param name="email">The email address to send email to; null if no email is to be sent.</param>
        /// <returns>A URL to redirect to if a confirmation is required.</returns>
        public string SendNotification(string userID, string notification, string email) {
            if (String.IsNullOrEmpty(userID)) {
                throw new ArgumentNullException("userID");
            }

            return SendNotification(new string[] { userID }, notification, email);
        }

        /// <summary>
        /// Sends a notification to the specified set of users, and optionally sends email.
        /// </summary>
        /// <param name="userIDs">The ID of the users to notify.</param>
        /// <param name="notification">The notification to send.</param>
        /// <param name="email">The email address to send email to; null if no email is to be sent.</param>
        /// <returns>A URL to redirect to if a confirmation is required.</returns>
        public string SendNotification(ICollection<string> userIDs, string notification, string email) {
            if ((userIDs == null) || (userIDs.Count == 0)) {
                throw new ArgumentNullException("userIDs");
            }
            if (String.IsNullOrEmpty(notification)) {
                throw new ArgumentNullException("notification");
            }

            StringBuilder userIDListBuilder = new StringBuilder();
            foreach (string id in userIDs) {
                if (userIDListBuilder.Length != 0) {
                    userIDListBuilder.Append(",");
                }
                userIDListBuilder.Append(id);
            }

            FacebookRequest request = new FacebookRequest(_session);
            request.Parameters["to_ids"] = userIDListBuilder.ToString();
            request.Parameters["notification"] = notification;
            if (String.IsNullOrEmpty(email) == false) {
                request.Parameters["email"] = email;
            }

            FacebookResponse response = request.InvokeMethod("notifications.send");
            if (response.Status == FacebookResponseStatus.Succeeded) {
                if (response.Result is string) {
                    return (string)response.Result;
                }
            }

            return null;
        }

        /// <summary>
        /// Sends a request or an invitation to the specified user.
        /// </summary>
        /// <param name="userID">The ID of the user to send a request.</param>
        /// <param name="requestType">The type of request.</param>
        /// <param name="content">The content of the request.</param>
        /// <param name="imageURL">The image associated with the request.</param>
        /// <param name="isInvitation">Whether the request is an invitation.</param>
        /// <returns>A URL to redirect to if a confirmation is required.</returns>
        public string SendRequest(string userID, string requestType, string content, string imageURL, bool isInvitation) {
            if (String.IsNullOrEmpty(userID)) {
                throw new ArgumentNullException("userID");
            }

            return SendRequest(new string[] { userID }, requestType, content, imageURL, isInvitation);
        }

        /// <summary>
        /// Sends a request or an invitation to the specified set of users.
        /// </summary>
        /// <param name="userIDs">The ID of the users to send a request.</param>
        /// <param name="requestType">The type of request.</param>
        /// <param name="content">The content of the request.</param>
        /// <param name="imageURL">The image associated with the request.</param>
        /// <param name="isInvitation">Whether the request is an invitation.</param>
        /// <returns>A URL to redirect to if a confirmation is required.</returns>
        public string SendRequest(ICollection<string> userIDs, string requestType, string content, string imageURL, bool isInvitation) {
            if ((userIDs == null) || (userIDs.Count == 0)) {
                throw new ArgumentNullException("userIDs");
            }
            if (String.IsNullOrEmpty(requestType)) {
                throw new ArgumentNullException("requestType");
            }
            if (String.IsNullOrEmpty(content)) {
                throw new ArgumentNullException("content");
            }
            if (String.IsNullOrEmpty(imageURL)) {
                throw new ArgumentNullException("imageURL");
            }

            StringBuilder userIDListBuilder = new StringBuilder();
            foreach (string id in userIDs) {
                if (userIDListBuilder.Length != 0) {
                    userIDListBuilder.Append(",");
                }
                userIDListBuilder.Append(id);
            }

            FacebookRequest request = new FacebookRequest(_session);
            request.Parameters["to_ids"] = userIDListBuilder.ToString();
            request.Parameters["type"] = requestType;
            request.Parameters["content"] = content;
            request.Parameters["image"] = imageURL;
            request.Parameters["invite"] = isInvitation ? "true" : "false";

            FacebookResponse response = request.InvokeMethod("notifications.sendRequest");
            if (response.Status == FacebookResponseStatus.Succeeded) {
                if (response.Result is string) {
                    return (string)response.Result;
                }
            }

            return null;
        }
    }
}
